package com.ctjsoft.collect;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试增强For循环
 * 增强for循环，底层是迭代器(可打断点进行查看)
 * @author: liaoyy
 * @date: Created in 2021/11/22 22:19
 */
public class ForLoopDemo {

    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("1");
        collection.add("2");
        collection.add("3");
        collection.add("4");

        //使用增强for循环遍历集合
        for (String s : collection) {
            System.out.println(s);
        }

        //增强for循环也可使用在数组上
        int[] nums = {1, 2, 3, 4, 5};
        for (int num : nums) {
            System.out.println(num);
        }

        //使用在Map上
        Map<String, String> map = new HashMap<>(3);
        map.put("1", "1");
        map.put("2", "1");
        map.put("3", "1");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry);
        }
    }
}
